(function() {
    'use strict';
    angular.module('weApp.controllers')
            //位置
            .controller('locationCtrl', ['$scope', '$rootScope', '$state', '$stateParams', '$timeout', '$http', 'weAppDataBase', '$ionicPopup', '$compile', 'showLinePageService', 'localStorageService', '$ionicLoading',
                function($scope, $rootScope, $state, $stateParams, $timeout, $http, weAppDataBase, $ionicPopup, $compile, showLinePageService, localStorageService, $ionicLoading) {
                    //设置一个键入延迟
                    var keyUpTimeout = null;

                    $scope.tag = $stateParams.tag;
                    $scope.tit = $stateParams.tit;
                    if ($scope.tit && $scope.tag) {
                        localStorageService.set("searchTag", JSON.stringify([$scope.tit, $scope.tag]));
                    } else {
                        var searchTag = JSON.parse(localStorageService.get("searchTag"));
                        $scope.tit = searchTag[0];
                        $scope.tag = searchTag[1];
                    }

                    if (!$rootScope.sAddr) {
                        var sAddr = JSON.parse(localStorageService.get("sAddr"));
                        $rootScope.sAddr = sAddr;
                    }

                    $scope.searchContext = {
                        // keyUpToSearch: false,
                        searchText: ''
                    }
                    $scope.searchLine = function() {
                        if (keyUpTimeout)
                            $timeout.cancel(keyUpTimeout);
                        keyUpTimeout = $timeout(function() {
                            if ($scope.searchContext.searchText != "") {
                                var getCityfromePos = "http://api.map.baidu.com/place/v2/suggestion?city_limit=true&output=json&ak=7vVOlMOKr03PaWX82WajF6mG&callback=JSON_CALLBACK&query=" + $scope.searchContext.searchText + "&region=" + $rootScope.cityCode;

                                $http.jsonp(getCityfromePos)
                                        .then(function(cityPos) {
                                            console.log(JSON.stringify(cityPos.data.result));
                                            // alert("当前详细地址:" + cityPos.data.result);
                                            var num = cityPos.data.result.length;

                                            if (cityPos.data.result.length > 0) {
                                                $scope.serData = [];
                                                var data = cityPos.data.result;
                                                for (var j = 0; j < num; j++) {
                                                    if (data[j].location) {
                                                        $scope.serData.push(data[j])
                                                    }
                                                }
                                            }

                                        }, function(error) {
                                            // $rootScope.appReadyPromise.reject(error);
                                        });
                            }
                        }, 300);

                    }
                    $scope.clickMap = function() {
                        console.log('clickMap');
                        $scope.tag = $stateParams.tag;
                        $scope.tit = $stateParams.tit;
                        if ($scope.tag == 'sta') {
                            var data = {tit: '请选择起点', tag: 'sta'};
                        } else {
                            var data = {tit: '请选择终点', tag: 'end'};
                        }
                        $state.go('map', data);
                    }
                    $scope.clickMy = function() {
                        console.log('#clickMy');
                        if (!$rootScope.sAddr) {
                            $ionicLoading.show({
                                template: '无法获取用户位置'
                            })

                            $timeout(function() {
                                $ionicLoading.hide();
                            }, 2000);
                            return false;
                        }
                        if ($scope.tag == 'sta') {
                            if ($rootScope.serLoc) {
                                $rootScope.serLoc.sta = $rootScope.sAddr.addr;
                                $rootScope.serLoc.sName = $rootScope.sAddr.addr.name;
                                if ($rootScope.serLoc.eName) {
                                    if ($rootScope.serLoc.sName == $rootScope.serLoc.eName) {
                                        $ionicLoading.show({
                                            template: '起点终点不能完全相同'
                                        })

                                        $timeout(function() {
                                            $ionicLoading.hide();
                                        }, 2000);
                                        return false;
                                    } else {
                                        $state.go('transfer', {data: $rootScope.serLoc});
                                        return false;
                                    }
                                }
                            } else {
                                $rootScope.sAddr.sta = '我的位置';
                                $rootScope.sAddr.end = '';
                            }
                        } else if ($scope.tag == 'end') {
                            if ($rootScope.serLoc) {
                                $rootScope.serLoc.end = $rootScope.sAddr.addr;
                                $rootScope.serLoc.eName = $rootScope.sAddr.addr.name;
                                if ($rootScope.serLoc.sName) {
                                    if ($rootScope.serLoc.sName == $rootScope.serLoc.eName) {
                                        $ionicLoading.show({
                                            template: '起点终点不能完全相同'
                                        })

                                        $timeout(function() {
                                            $ionicLoading.hide();
                                        }, 2000);
                                        return false;
                                    } else {
                                        $state.go('transfer', {data: $rootScope.serLoc});
                                        return false;
                                    }
                                }
                            } else {
                                $rootScope.sAddr.sta = '';
                                $rootScope.sAddr.end = '我的位置';
                            }
                        }
                        $state.go('trans', {data: $rootScope.serLoc});
                    }
                    $scope.serCli = function(data) {
                        // console.log(data);
                        if ($scope.tag == 'sta') {
                            $scope.searchContext.searchText = data.name;

                            if ($rootScope.serLoc) {
                                $rootScope.serLoc.sta = {name: data.name, location: data.location};
                                $rootScope.serLoc.sName = data.name;
                            } else {
                                $rootScope.serLoc = {
                                    sta: {name: data.name, location: data.location},
                                    sName: data.name
                                }
                            }
                            if ($rootScope.sAddr.end == "我的位置" && $rootScope.sAddr.addr) {
                                $rootScope.serLoc.end = $rootScope.sAddr.addr;
                                $rootScope.serLoc.eName = $rootScope.sAddr.addr.name;
                                $state.go('transfer', {data: $rootScope.serLoc});
                                return false;
                            }
                        } else if ($scope.tag == 'end') {
                            $scope.searchContext.searchText = data.name;

                            if ($rootScope.serLoc) {
                                $rootScope.serLoc.end = {name: data.name, location: data.location};
                                $rootScope.serLoc.eName = data.name;
                            } else {
                                $rootScope.serLoc = {
                                    end: {name: data.name, location: data.location},
                                    eName: data.name
                                }
                            }
                            if ($rootScope.sAddr.sta == "我的位置" && $rootScope.sAddr.addr) {
                                $rootScope.serLoc.sta = $rootScope.sAddr.addr;
                                $rootScope.serLoc.sName = $rootScope.sAddr.addr.name;
                                $state.go('transfer', {data: $rootScope.serLoc});
                                return false;
                            }
                        }

                        if ($rootScope.serLoc.sName && $rootScope.serLoc.eName) {
                            if ($rootScope.serLoc.sName == $rootScope.serLoc.eName) {
                                $ionicLoading.show({
                                    template: '起点终点不能完全相同'
                                })

                                $timeout(function() {
                                    $ionicLoading.hide();
                                }, 2000);
                                return false;
                            } else {
                                $state.go('transfer', {data: $rootScope.serLoc});
                                return false;
                            }
                        } else {
                            $state.go('trans', {data: $rootScope.serLoc});
                        }
                    }
                    
//                    $scope.$on("$ionicView.enter", function(event, data) {
//                        setTimeout(function() {
//                            $("#s-input").focus();
//                        }, 100)
//                    })
                    setTimeout(function() {
                        $("#s-input").trigger("click").focus();
                    }, 100)
                }]);
})();